<?php

namespace app\index\controller;

use app\admin\model\Article;
use app\common\controller\Frontend;
use app\common\model\Category;

class Product extends Frontend
{
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    /**
     * 藏品展示
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $cateInfo = Category::where(['nickname' => '/product', 'pid' => 0])->field('id, pid, name, nickname, type, flag')->find();
        $cateId = $this->request->param('cate_id');
        $where = ['category_type' => 'goods'];
        if ($cateId) {
            $where['category_id'] = $cateId;
            $cateInfo = Category::where(['id' => $cateId])->field('id, pid, name, nickname, type, flag')->find();
        }
        $paginate = Article::where(['status_data' => 1])
            ->where($where)
            ->order('weigh desc, id desc')
            ->field('id, name, category_id, image, view_num, content, tags_data, status_data, createtime, weigh')
            ->paginate(9, false, ['query' => $this->request->param()]);
        $this->assign('cateInfo', $cateInfo);
        $this->assign('page', $paginate->render());
        $this->assign('paginate', $paginate);
        return $this->fetch();

    }

    /**
     * 详情
     * @return mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function detail()
    {
        $detailId = $this->request->param('id');
        $detailInfo = Article::find($detailId)->hidden(['deletetime', 'weigh', 'status_data']);
        if ($detailInfo) {
            $detailInfo = $detailInfo->toArray();
            $detailInfo['category_name'] = Category::where(['id' => $detailInfo['category_id']])->value('name');
        }
        $this->assign('detailInfo', $detailInfo);
        return $this->fetch('product/detail');
    }
}